//
//  MenuVC.m
//  dianfan
//
//  Created by Ye Jin on 15/11/9.
//  Copyright © 2015年 Ye Jin. All rights reserved.
//

#import "MenuVC.h"
#import <BmobSDK/bmob.h>
#import "submitVC.h"
#import "logoutVC.h"
#import "MBProgressHUD.h"
#import "Dish.h"

@interface MenuVC ()

@property (nonatomic) NSMutableDictionary *foodDict;

@end

@implementation MenuVC

- (void)logout
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    logoutVC *lvc = [sb instantiateViewControllerWithIdentifier:@"logout"];
    [self.navigationController pushViewController:lvc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.foodDict = [[NSMutableDictionary alloc] init];
    //转菊花
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    hud.labelText=@"loading";
    BmobUser *bUser = [BmobUser getCurrentUser];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:bUser.username
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(logout)];
    
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"Menu"];
    //BmobQuery *bquery2 = [BmobQuery queryWithClassName:@"Restaurant"];
    //[bquery2 whereKey:@"objectId" equalTo:self.restaurantID];
    BmobObject *rest = [BmobObject objectWithoutDatatWithClassName:@"Restaurant" objectId:self.restaurant.ID];
    [bquery whereKey:@"restaurant" equalTo:rest];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@",[[error userInfo] objectForKey:@"error"]] message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        for (BmobObject *obj in array) {
            [self.foodDict setValue:@"" forKey:[obj objectForKey:@"dishType"]];
        }
        for (NSString *key in [self.foodDict allKeys]) {
            NSMutableArray *valueArray = [[NSMutableArray alloc] init];
            for (BmobObject *obj in array) {
                if ([key isEqualToString: [obj objectForKey:@"dishType"]]) {
                    Dish *dish = [[Dish alloc] init];
                    dish.ID = [obj objectForKey:@"objectId"];
                    dish.name = [obj objectForKey:@"dishName"];
                    [valueArray addObject:dish];
                }
            }
            [self.foodDict setObject:valueArray forKey:key];
        }
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[self.foodDict allKeys] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *foodType = [[self.foodDict allKeys] objectAtIndex:section];
    return foodType;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *detailFoods = [self.foodDict objectForKey:[[self.foodDict allKeys] objectAtIndex:section]];
    return [detailFoods count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *detailFoods = [self.foodDict objectForKey:[[self.foodDict allKeys] objectAtIndex:indexPath.section]];
    static NSString *ID = @"detailFoods";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    Dish *dish = [detailFoods objectAtIndex:indexPath.row];
    cell.textLabel.text = dish.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *detailFoods = [self.foodDict objectForKey:[[self.foodDict allKeys] objectAtIndex:indexPath.section]];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    submitVC *svc = [sb instantiateViewControllerWithIdentifier:@"submit"];
    svc.restaurant = self.restaurant;
    svc.dish = [detailFoods objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:svc animated:YES];
}

@end
